<template>
    <v-details :detailData="data.detailData" :inputConfig="inputConfig" :rules="rules" @pop="setdata"
               ref="input"></v-details>
</template>

<script>

import details from 'commons/Detail'
import baseMixin from "@/mixins/BaseMixin";

export default {
    name: "",
    mixins: [baseMixin],
    props: {
        data: {
            type: Object
        }
    },
    data() {
        return {
            rules: {
                transferlineno: [
                    {required: true, trigger: 'blur'},
                ],
                sku: [
                    {required: true, validator: this.sku, trigger: 'blur'},
                ],
                expectedqty: [
                    {required: true, validator: this.expectedqty, trigger: 'blur'},
                ],
            },
            url: `/querySkuOfStorerkey/?/${this.$store.state.wms}/undefined`,
            options: [],
            addflag: false
        }
    },
    computed: {
        inputConfig() {
            return [
                [
                    {
                        type: "input",
                        label: this.$t('outbound.userlinenumber'),
                        props: "transferlineno",
                        disabled: true
                    },
                    {
                        type: "table",
                        label: this.$t('config.sku'),
                        props: "sku",
                        tableConfig: [
                            {
                                label: this.$t('config.sku'),
                                prop: "sku",
                                input: "input",
                            },
                            {
                                label: this.$t('common.desc'),
                                prop: "descr",
                                input: "input",
                            },
                            {
                                label: this.$t('config.shipper'),
                                prop: "storerkey",
                                input: "input",
                            },
                            {
                                label: this.$t('config.packaging'),
                                prop: "packkey",
                                input: "input",
                            },
                            {
                                label: this.$t('config.boxGroup'),
                                prop: "cartongroup",
                                input: "select",
                                options: this.getOptions("queryCartonizationDropDown")
                            },
                            {
                                label: this.$t('config.allocaStrategy'),
                                prop: "strategykey",
                                input: "select",
                                options: this.getOptions("queryAllocatestrategyDropDown")
                            },
                            {
                                label: this.$t('config.putawayStrategy'),
                                prop: "putawaystrategykey",
                                input: "select",
                                options: this.getOptions("queryPutawaystrategyDropDown")
                            },
                            {
                                label: this.$t('in.serialnumbe'),
                                prop: "serialnumbe",
                                input: "input",
                            },
                            {
                                label: this.$t('config.batchValidate'),
                                prop: "lottablevalidationkey",
                                input: "select",
                                options: this.getOptions("queryLottablevalidationDropDown")
                            },
                            {
                                label: this.$t('config.area'),
                                prop: "putawayzone",
                                input: "select",
                                options: this.getOptions("queryPutawayzoneDropDown")
                            },
                        ],
                        url: this.url
                    },
                    {
                        type: "input",
                        label: this.$t('common.desc'),
                        props: "descr",
                        disabled: true
                    },
                ],
                [
                    {
                        type: "input",
                        label: this.$t('in.externreceiptkey2'),
                        props: "externalno",
                        disabled: true
                    },
                    {
                        type: "select",
                        label: this.$t('config.measureUnit'),
                        props: "uom",
                        options: this.options
                    },
                    {
                        type: "input",
                        label: this.$t('out.cartonizationkey'),
                        props: "fromid",
                    },
                ],
                [
                    {
                        type: "input",
                        label: this.$t('ex.fromstorerkey'),
                        props: "fshippers",
                        disabled: true
                    },
                    {
                        type: "input",
                        label: this.$t('in.qtyexpected'),
                        props: "expectedqty",
                    },
                    {
                        type: "input",
                        label: this.$t('out.tocartonizationkey'),
                        props: "toid",
                    },
                ],
                [
                    {
                        type: "input",
                        label: this.$t('ex.tostorerkey'),
                        props: "tshippers",
                        disabled: true
                    }
                ]
            ]

        }
    },
    methods: {
        expectedqty(rule, value, callback) {
            if (value === '') {
                callback(new Error());
            } else {
                var re = /^[0-9]*$/;
                let te = value;
                let flag = re.test(te)
                if (flag) {
                    if (value < 0) {
                        this.$message.error(this.$t('app.inputGreaterZeroNumberPlease'))
                        this.data.detailData[rule.field] = 0
                        callback(new Error());
                    } else {
                        callback();
                    }
                } else {
                    this.$message.error(this.$t('app.inputGreaterZeroNumberPlease'))
                    this.data.detailData[rule.field] = 0
                    callback(new Error());
                }
            }
        },
        sku(rule, value, callback) {
            if (value === '') {
                callback(new Error());
            } else {
                this.$api.get(`/sku/isExist/${value}/${this.$store.state.wms}/${this.$parent.$parent.$refs.detail.detailData.fshippers}`).then(res => {
                    
                        if (res.bean) {
                            this.options = this.getOptions(`queryFfdefaultuomDropDown/${res.bean.packkey}`);
                            this.data.detailData.descr = res.bean.descr;
                            callback();
                        } else {
                            // this.$message.error(`不存在的货品:${value}`);
                            callback()
                        }
                    
                }).catch(e => {
                    this.$message.error(e);
                    callback(new Error())
                })
            }
        },
        setdata(data, id, close, listId) {
            this.data.detailData.sku = data.sku;
            this.options = this.getOptions(`queryFfdefaultuomDropDown/${data.packkey}`);
            this.data.detailData.descr = data.descr;
            close(false);
        }
    },
    mounted() {
    },
    watch: {
        '$parent.$parent.$refs.detail.detailData.fshippers': {
            handler(data) {
                if (data == "") {
                    data = undefined;
                }
                this.url = `/querySkuOfStorerkey/?/${this.$store.state.wms}/${data}`;
                this.data.detailData.fshippers = data;
            },
            immediate: true,
            deep: true
        },
        '$parent.$parent.$refs.detail.detailData.tshippers': {
            handler(data) {
                this.data.detailData.tshippers = data;
            },
            immediate: true,
            deep: true
        },
        '$parent.$parent.$refs.detail.detailData.externalno': {
            handler(data) {
                this.data.detailData.externalno = data;
            },
            immediate: true,
            deep: true
        }
    },
    components: {
        'v-details': details
    }
}
</script>

<style scoped>

</style>
